<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增门店库存')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-storeinventory-add">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">供货仓库名称：</label>
                <div class="col-sm-8">
                    <select id="deliveryWarehouseName" name="deliveryWarehouseName" class="form-control" required></select>
                </div>
            </div>
            <!--  门店编号  -->
            <input type="hidden" id="shopId" name="shopId" class="form-control" required readonly/>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">门店名称：</label>
                <div class="col-sm-8">
                    <select id="shopName" name="shopName" class="form-control" required>
                        <option disabled selected>请先选择仓库</option>
                    </select>
                </div>
            </div>
            <!-- 水果种类编号 -->
            <input type="hidden" id="fruittypesId" name="fruittypesId" class="form-control" required readonly>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">水果种类名称：</label>
                <div class="col-sm-8">
                    <select id="fruittypesName" name="fruittypesName" class="form-control" required>
                        <option disabled selected>请先选择门店</option>
                    </select>
                </div>
            </div>
            <!-- 水果编号 -->
            <input type="hidden" id="fruitId" name="fruitId" class="form-control" required readonly>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">水果名称：</label>
                <div class="col-sm-8">
                    <select id="fruitName" name="fruitName" class="form-control" required>
                        <option disabled selected>请先选择水果种类</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">重量(kg)：</label>
                <div class="col-sm-8">
                    <input name="number" placeholder="请输入进货量(kg)" class="form-control" type="text" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script>
        $(document).ready(function() {
            selectWareHouseId(); // 查询所有的仓库信息

        });
        // 查询所有仓库信息
        var wareHouseObj = {};
        function selectWareHouseId() {
            $.ajax({
                url: "/inventory/storeinventory/selectWareHouseId",
                dataType: "json",
                type: "post",
                success: function (resp) {
                    wareHouseObj = {}; // 清空对象
                    if (resp.wareHouse.length > 1) {
                        $("<option disabled selected></option>").html("请选择仓库").appendTo("#deliveryWarehouseName");
                        for (let i = 0; i < resp.wareHouse.length; i++) {
                            let wareHouse = resp.wareHouse[i];
                            console.log("仓库名称" + (i+1) + ":"  + wareHouse.wName);
                            wareHouseObj[wareHouse.wName] = wareHouse.wNumber; // warehouseWarehouse.wName作为键，warehouseWarehouse.wNumber作为值存入对象中
                            $("<option></option>").html(wareHouse.wName).appendTo("#deliveryWarehouseName");
                        }
                        console.log("所有的仓库:");
                        console.log(wareHouseObj)
                    } else {
                        for (let i = 0; i < resp.wareHouse.length; i++) {
                            let wareHouse = resp.wareHouse[i];
                            console.log(wareHouse);
                            console.log("仓库编号:" + wareHouse.wNumber);
                            console.log("仓库名称:"  + wareHouse.wName);
                            $("<option></option>").html(wareHouse.wName).appendTo("#deliveryWarehouseName");
                            selectShopId(); // 查询所有门店编号
                            selectWareHouseStockFruit(wareHouse.wNumber, wareHouse.wName); // 根据选中的仓库名称的编号查询水果编号
                        }
                    }
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }

        // 监听仓库名称是否选中
        $(document).ready(function () {
            $("#deliveryWarehouseName").on('change', function () {
                $("#fruittypesName").empty(); // 清空水果种类下拉框的所有选项
                $("<option disabled selected></option>").html("请先选择门店").appendTo("#fruittypesName");
                $("#fruitName").empty();// 清空水果下拉框的所有选项
                $("<option disabled selected></option>").html("请先选择水果种类").appendTo("#fruitName");
                $("#shopId").val(""); // 每更改一次仓库就清空一次门店编号
                $("#fruittypesId").val(""); // 每更改一次仓库就清空一次水果种类编号
                $("#fruitId").val(""); // 每更改一次仓库就清空一次水果编号
                let wName = $("#deliveryWarehouseName").val(); // 获取选中的仓库名称
                console.log("选中的仓库:" + wName);
                if (wName !== null) {
                    var wareHouseWnumber = wareHouseObj[wName]; // 根据选则的仓库名称找出对应的仓库编号
                    selectShopId(); // 查询所有门店编号
                    selectWareHouseStockFruit(wareHouseWnumber, wName); // 根据选中的仓库名称的编号查询水果编号
                }
            })
        })

        // 查询对应仓库中有的水果
        let warehousestockFruitMap = [] // 创建一个仓库的水果的空集合
        let warehousestockFruitTypeMap = []
        function selectWareHouseStockFruit(wareHouseWnumber, wName) {
            $.ajax({
                url: "/inventory/storeinventory/selectWareHouseStockFruit",
                data: {
                    "wId": wareHouseWnumber
                },
                dataType: "json",
                type: "post",
                success: function (resp) {
                    warehousestockFruitMap = []; // 清空集合
                    warehousestockFruitTypeMap = []; // 清空集合
                    for (let i = 0; i < resp.wareHouseList.length; i++) {
                        let wareHouseList = resp.wareHouseList[i];
                        warehousestockFruitMap[i] = wareHouseList.fruitId; // 将查到的水果编号插入到集合中
                        if (!warehousestockFruitTypeMap.includes(wareHouseList.fruittypesId)) {
                            warehousestockFruitTypeMap[i] = wareHouseList.fruittypesId;
                        }
                    }
                    console.log(wName + "仓库中有的水果种类集合:");
                    console.log(warehousestockFruitTypeMap);
                    console.log(wName + "仓库中有的水果集合:");
                    console.log(warehousestockFruitMap);
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }

        // 查询所有门店编号
        var shopObj = {};
        function selectShopId() {
            $.ajax({
                url: "/inventory/storeinventory/selectShopId",
                dataType: "json",
                type: "post",
                success: function (resp) {
                    $("#shopName").empty();
                    shopObj = {}; // 清空对象
                    if (resp.shop.length > 1) {
                        $("<option disabled selected></option>").html("请选择门店").appendTo("#shopName");
                        for (let i = 0; i < resp.shop.length; i++) {
                            let shop = resp.shop[i];
                            shopObj[shop.sName] = shop.sNumber; // shop.sName作为键，shop.sNumber作为值存入对象中
                            $("<option></option>").html(shop.sName).appendTo("#shopName");
                        }
                        console.log("所有的门店:");
                        console.log(shopObj);
                    } else {
                        for (let i = 0; i < resp.shop.length; i++) {
                            let shop = resp.shop[i];
                            console.log("门店编号:" + shop.sNumber)
                            console.log("门店名称:" + shop.sName)
                            $("<option></option>").html(shop.sName).appendTo("#shopName");
                            $("#shopId").val(shop.sNumber); // 将选中的门店的编号插入输入框中
                            selectFruitTypes(); // 查询水果种类
                            selectFruitId(shop.sNumber, shop.sName); // 根据选中的门店名称的编号查询水果编号
                        }
                    }

                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }

        // 监听门店名称是否选中
        $(document).ready(function () {
            $("#shopName").on('change', function () {
                $("#fruitName").empty();// 清空水果下拉框的所有选项
                $("<option disabled selected></option>").html("请先选择水果种类").appendTo("#fruitName");
                $("#fruittypesId").val(""); // 每更改一次门店就清空一次水果种类编号
                $("#fruitId").val(""); // 每更改一次门店就清空一次水果编号
                let shopName = $("#shopName").val(); // 获取选中的门店名称
                console.log("选中的门店:" + shopName)
                let shopId = shopObj[shopName]; // 根据选中的门店名称 获取对应的门店编号
                $("#shopId").val(shopId); // 将选中的门店的编号插入输入框中
                if (shopId !== null) {
                    selectFruitTypes(); // 查询水果种类
                    selectFruitId(shopId, shopName); // 根据选中的门店名称的编号查询水果编号
                }
            })
        })

        // 查询水果种类编号和水果种类名称
        var fruittypesObj = {}; // 创建一个空对象
        function selectFruitTypes() {
            $.ajax({
                url: "/inventory/fruittypes/name",
                dataType: "json",
                type: "post",
                success: function (resp) {
                    $("#fruittypesName").empty();
                    fruittypesObj = {}; // 清空对象
                    $("<option disabled selected></option>").html("请选择水果种类").appendTo("#fruittypesName");
                    for (let i = 0; i < resp.fruittypes.length; i++) {
                        var fruittypes = resp.fruittypes[i];
                        if (warehousestockFruitTypeMap.includes(fruittypes.code)) { // 如果仓库中有改水果种类的水果,则将该种类插入下拉框
                            fruittypesObj[fruittypes.name] = fruittypes.code; // 将fruittypes.name作为键，fruittypes.code作为值存入对象中
                            $("<option></option>").html(fruittypes.name).appendTo("#fruittypesName");
                        }
                    }
                    console.log($("#deliveryWarehouseName").val() + "仓库中有的水果种类:");
                    console.log(fruittypesObj);
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }


        //监听水果种类名称,插入对应的水果种类编号
        $(document).ready(function () {
            $("#fruittypesName").on('change', function () {
                $("#fruitId").val(""); // 每更改一次水果种类就清空一次水果编号
                var fruittypesName = $("#fruittypesName").val(); // 获取选中的水果种类的名称
                console.log("选中的水果种类:" + fruittypesName);
                var fruittypesCode = fruittypesObj[fruittypesName]; // 根据选中的水果种类名称 获取对应的水果种类编号
                $("#fruittypesId").val(fruittypesCode); // 将选中的水果种类的编号插入输入框中
                selectFruits(fruittypesCode, fruittypesName); // 根据水果类型编号查询水果编号和水果名称 (只查询状态正常的水果)
            })
        })

        // 根据门店编号查询水果编号
        let shopIdOfFruitIdMap = []; // 创建一个空集合
        function selectFruitId(shopId, shopName) {
            $.ajax({
                url: "/inventory/storeinventory/selectFruitId",
                data: {
                    "shopId": shopId
                },
                dataType: "json",
                type: "post",
                success: function (resp) {
                    shopIdOfFruitIdMap = []; // 清空集合
                    for (let i = 0; i < resp.shopIdOfFruitId.length; i++) {
                        shopIdOfFruitIdMap[i] = resp.shopIdOfFruitId[i]; // 将查出来的水果编号插入集合中
                    }
                    console.log(shopName + "门店中已有的水果集合:");
                    console.log(shopIdOfFruitIdMap);
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }

        // 根据水果类型编号查询水果编号和水果名称 (只查询状态正常的水果)
        var fruitObj = {};
        function selectFruits(fruittypesCode, fruittypesName) {
            $.ajax({
                url: "/inventory/fruits/selectFruit",
                dataType: "json",
                data: {
                    "fruittypesId": fruittypesCode,
                    "isStatus" : 1
                },
                type: "post",
                success: function (resp) {
                    $("#fruitName").empty();
                    fruitObj = {}; // 清空对象
                    $("<option disabled selected></option>").html("请选择水果").appendTo("#fruitName");
                    for (let i = 0; i < resp.fruits.length; i++) {
                        var fruits = resp.fruits[i]
                        if (!shopIdOfFruitIdMap.includes(fruits.code) && warehousestockFruitMap.includes(fruits.code)) {
                            // 如果门店里没有该水果, 并且该仓库中有该水果且状态为正常 则添加到水果名称下拉框中
                            fruitObj[fruits.name] = fruits.code; // fruits.name作为键，fruits.code作为值存入对象中
                            $("<option></option>").html(fruits.name).appendTo("#fruitName");
                        }
                    }
                    console.log("门店中"+ fruittypesName + "里没有的水果集合:")
                    console.log(fruitObj);
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
        // 监听水果名称下拉框,如果选择,则插入对应的水果编号
        $(document).ready(function () {
            $("#fruitName").on('change', function () {
                var fruitName = $("#fruitName").val(); // 获取选中的水果名称
                console.log("选中的水果:" + fruitName);
                var fruitsCode = fruitObj[fruitName]; // 根据选中的水果名称 获取对应的水果编号
                $("#fruitId").val(fruitsCode); // 将选中的水果的编号插入输入框中
            })
        })
    </script>
    <script th:inline="javascript">
        var prefix = ctx + "inventory/storeinventory"
        $("#form-storeinventory-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-storeinventory-add').serialize());
            }
        }
    </script>
</body>
</html>